#import "ARLCommonUtils.h"

BOOL mcp_is_same_queue(dispatch_queue_t queue) {
    return strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(queue)) == 0;
}

@implementation ARLCommonUtils

+ (NSString *)sortDictByNumeric2Str:(NSDictionary *)sourceDict {
    NSArray *keys = [sourceDict allKeys];
    NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    NSString *sortedStr = @"";
    
    for (NSString *key in sortedKeys) {
        NSString *kvStr = [NSString stringWithFormat:@"%@=%@&", key, [sourceDict objectForKey:key]];
        sortedStr = [NSString stringWithFormat:@"%@%@", sortedStr, kvStr];
    }
    
    sortedStr = [sortedStr substringToIndex:sortedStr.length-1];
    
    return sortedStr;
}


#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
const char* mcp_jailbreak_tool_pathes[] = {
    "/Applications/Cydia.app",
    "/Library/MobileSubstrate/MobileSubstrate.dylib",
    "/bin/bash",
    "/usr/sbin/sshd",
    "/etc/apt"
};

+ (BOOL)isJailBreak {
    NSString *USER_APP_PATH =@"/User/Applications/";
    if ([[NSFileManager defaultManager] fileExistsAtPath:USER_APP_PATH]) {
        //这个是利用不越狱的机器没有这个权限来判定的
        return YES;
    }
    //ios8以后检查该目录
    NSString *USER_APP_PATH_Later = @"/User/Containers/Bundle/Application/";
    if ([[NSFileManager defaultManager]fileExistsAtPath:USER_APP_PATH_Later]) {
        return YES;
    }
    for (int i=0; i<ARRAY_SIZE(mcp_jailbreak_tool_pathes); i++) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:mcp_jailbreak_tool_pathes[i]]]) {
            return YES;
        }
    }
    return NO;
}

@end
